//
//  StoreHotelTableHeaderView.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/20.
//  Copyright © 2020 王洋. All rights reserved.
//

#define SH_bannerHeight   TTScreenWidth
#define SH_infoHeight     300
#define SH_distance       -100

#import "StoreHotelTableHeaderView.h"

@interface StoreHotelTableHeaderView ()

@property (nonatomic, strong) UIImageView *bgImageView;

@end

@implementation StoreHotelTableHeaderView

+ (CGFloat)calculateHeightWithModel:(StoreHotelDetailModel *)model {

    CGFloat bannerHeight = SH_bannerHeight;
    CGFloat infoViewHeight = [StoreHotelInfoView calculateHeightWithModel:model];
    CGFloat distance = SH_distance;
    return bannerHeight + infoViewHeight + distance;
}

- (instancetype)init {
    self = [super init];
    if (self) {

        [self layout];
    }
    return self;
}

- (void)fillHeadWithModel:(StoreHotelDetailModel *)model {
    
    [self.infoView fillHeadWithModel:model];

    NSMutableArray *photos = @[].mutableCopy;
    for (int i = 0; i < model.seller.coverPaths.count; i++) {
        
        CommonImageModel *imgModel = model.seller.coverPaths[i];
        if (imgModel.url.length > 0) {
            [photos addObject:imgModel.url];
        }
    }
    
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:photos.firstObject] placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageRetryFailed];
}

- (void)layout {


    [self addSubview:self.bgImageView];
    [self addSubview:self.mediaBarView];
    [self addSubview:self.infoView];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(SH_bannerHeight);
    }];
    
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.bgImageView.mas_bottom).offset(SH_distance);
    }];
    
    [self.mediaBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(self.infoView.mas_top).offset(-15);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];

    WEAKSELF
    self.infoView.StoreHotelInfoViewBlock = ^(StoreHotelInfoActionType type) {
      
        switch (type) {
            case StoreHotelInfoActionType_wuyouxuan:{
                
                if (weakSelf.StoreHotelDetailTableHeaderViewBlock) {
                    weakSelf.StoreHotelDetailTableHeaderViewBlock(StoreHotelDetailTableHeaderType_wuyouxuan);
                }
            } break;
            case StoreHotelInfoActionType_moreInfo:{
                
            } break;
            case StoreHotelInfoActionType_mapNavi:{
                if (weakSelf.StoreHotelDetailTableHeaderViewBlock) {
                    weakSelf.StoreHotelDetailTableHeaderViewBlock(StoreHotelDetailTableHeaderType_mapNavi);
                }
            } break;
            case StoreHotelInfoActionType_im: {
                if (weakSelf.StoreHotelDetailTableHeaderViewBlock) {
                    weakSelf.StoreHotelDetailTableHeaderViewBlock(StoreHotelDetailTableHeaderType_im);
                }
                
            } break;
            case StoreHotelInfoActionType_hotelAlbum:{
                
            } break;
            default:
                break;
        }
    };
    
    self.bgImageView.userInteractionEnabled = YES;
    [self.bgImageView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
            
        if (weakSelf.StoreHotelDetailTableHeaderViewBlock) {
            weakSelf.StoreHotelDetailTableHeaderViewBlock(StoreHotelDetailTableHeaderType_hotelAlbum);
        }
    }];
    
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(0, 1);

    layer.colors = @[(__bridge id)[UIColorHex(0xf1f1f1) colorWithAlphaComponent:0].CGColor, (__bridge id)[UIColorHex(0xf1f1f1) colorWithAlphaComponent:1].CGColor];

    layer.frame = CGRectMake(0, SH_bannerHeight*2/3, TTScreenWidth, SH_bannerHeight/3);
    [self.bgImageView.layer insertSublayer:layer atIndex:0];
}

- (void)layoutSubviews {
    [super layoutSubviews];
            
    [self.infoView pp_clipCorners:UIRectCornerAllCorners radius:10];
}

- (StoreHotelInfoView *)infoView {
    
    if (!_infoView) {
        _infoView = [[NSBundle mainBundle] loadNibNamed:@"StoreHotelInfoView"owner:nil options:nil].firstObject;
    }
    return _infoView;
}

- (DetailBannerMediaTabView *)mediaBarView {
    if (!_mediaBarView) {
        _mediaBarView = [[NSBundle mainBundle] loadNibNamed:@"DetailBannerMediaTabView"owner:nil options:nil].firstObject;
    }
    return _mediaBarView;
}

- (UIImageView *)mengbanImgV {
    
    if (!_mengbanImgV) {
        _mengbanImgV = [[UIImageView alloc] init];
        [_mengbanImgV setImage:[UIImage imageNamed:@"store_mengban"]];
    }
    return _mengbanImgV;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.clipsToBounds = YES;
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bgImageView;
}

@end
